﻿using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools;
using Microsoft.Office.Tools.Excel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ExcelAddIn1
{
    public partial class ThisAddIn
    {
        UserControl1 uc1;
        CustomTaskPane ctp1;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            //Debug.WriteLine("该AddIn已加开始加载了");
            //Common.ExcelApp = this.Application;

            Common.ExcelApp = Globals.ThisAddIn.Application;

            if (Common.ExcelApp != null) {
                //Debug.WriteLine(Common.ExcelApp);
                Debug.WriteLine("可以获取到ExcelApp的对象");
                //Debug.WriteLine(Common.ExcelApp);
            }

            if (Common.ExcelApp.ActiveWorkbook != null)
            {
                Debug.WriteLine("可以获取到活动工作表");
                //Debug.WriteLine(Common.ExcelApp.ActiveWorkbook);
            }

            if (Common.ExcelApp.ActiveSheet!=null)
            {
                Debug.WriteLine("可以获取到活动单元表");
                //Debug.WriteLine(Common.ExcelApp.ActiveSheet);
            }

            //取消对于工作表的保存修改的代码程序
            //Common.ExcelApp.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);

            Common.cmb = Common.ExcelApp.CommandBars["task pane"];
            uc1 = new UserControl1();
            //uc1.Show();
            //Console.WriteLine("插件启动了");
            //Console.WriteLine("This is First Time.");
            ctp1 = Globals.ThisAddIn.CustomTaskPanes.Add(uc1, "VSTO");
            Debug.WriteLine(ctp1.Control.Name);
            //ctp1.Visible = true;
            ctp1.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            ctp1.Width = 400;
            Debug.WriteLine("该AddIn已加开始加载了");

        }

        private void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
        {
            Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
            Excel.Range firstRow = activeWorksheet.Range["A1"];
            firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
            Excel.Range newFirstRow = activeWorksheet.Range["A1"];
            newFirstRow.Value2 = "This text was added by using code.";
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
